<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.css" />
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css" />
	<script src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
	<script src="/js/jq.js"></script>
	<script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script src="/js/common.js"></script>
	<script src="/layui/layui.js"></script>
	<script src="/js/dict.js"></script>
	<script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script src="/js/my/ztree-menu.js"></script>
	<script src="/js/my/permission.js"></script>
	<script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
	<script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
	<script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<input type="hidden" id="id" name="id">
			<input type="hidden" id="parentId" name="parentId">
			<div class='form-group'>
				<label class='col-md-4 control-label'>子项目名称：</label>
				<div class='col-md-8'>
					<input class="form-control" placeholder="项目名称" name="name" id="name"
						   data-bv-notempty='true' data-bv-notempty-message='项目名称 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>项目描述：</label>
				<div class='col-md-8'>
                    <textarea rows="5" class="form-control" placeholder="项目描述" name="description" id="description"
							  data-bv-notempty='true' data-bv-notempty-message='项目描述 不能为空'></textarea>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>负责人：</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='executorId' id='executorId' data-bv-notempty='true' data-bv-notempty-message='负责人 不能为空'>
					<input style="width: 90%;float: left;" class='form-control' placeholder='负责人' type='text'
						   name='executorName' id='executorName' data-bv-notempty='true' data-bv-notempty-message='负责人 不能为空'
						   readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;"
							class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields( 'user', 'user', '用户', true, false, 'executorId:id', 'executorName:nickname')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>成员：</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='participantId' id='participantId' data-bv-notempty='true' data-bv-notempty-message='成员 不能为空'>
					<input style="width: 90%;float: left;" class='form-control' placeholder='成员' type='text'
						   name='participantName' id='participantName' data-bv-notempty='true' data-bv-notempty-message='成员 不能为空'
						   readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;"
							class="layui-btn layui-btn-sm"
							onclick="projectList( )">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>项目等级：<i title="项目紧急程度" class="layui-icon layui-icon-tips" style="font-size: 10px; color: #737373;"></i> </label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="grade" id="grade"
							title="项目等级" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>截止日期：</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='截止日期' type='text' name='deadline' id='deadline'
						   data-bv-notempty='true' data-bv-notempty-message='截止日期 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>项目状态:</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="status" id="status"
							title="项目状态" data-live-search="true"></select>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>
		</fieldset>
	</form>
</div>

<script type="text/javascript">
    $(function () {
        $('#form').bootstrapValidator();
    });

    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#deadline'
        });
    });
    var grade = showDictSelect('grade', 'dept_level', '', '1');
    var status = showDictSelect('status', 'task_status', '', '1');

    function projectList(){
        let parentId = $("#parentId").val();
        let flushFields = ['participantId:id', 'participantName:nickname'];
        let path = '/pages/user/userTableForTms.html?isAll=true&isMultiSelect=true&projectId=' + parentId;

        let echo = []; //传输的数组
        let count = 0; //传输的数组长度
        let val = $('#' + flushFields[0].split(':')[0]).val().trim();
        if (val) {
            count = val.split(';').length;
        }

        for (let i = 0; i < count; i++) {
            let item = {};
            $.each(flushFields, function (j, field) {
                let formField = field.split(':')[0];
                let entityField = field.split(':')[1];
                item[entityField] = $('#' + formField).val().split(';')[i];
            });
            echo[i] = item;
        }

        path += '&echo=' + encodeURIComponent(JSON.stringify(echo));

        layer.open({
            type: 2,
            title: '项目成员列表',
            shadeClose: true, //是否点击阴影关闭层
            area: [($(window).width() * 0.8) + 'px', ($(window).height() - 30) + 'px'],
            offset: 'auto', //rb右下角弹出
            anim: 0,
            content: [path, 'yes'], //iframe的url，no代表不显示滚动条
            fixed: true, // 是否固定
            maxmin: true,
            btn: ['确定', '清除', '取消'],
            yes: function (index, layero) {
                // let rows = layero.find("iframe")[0].contentWindow.example.rows('.success').data();
                let selectedItems = $(layero.find("iframe")[0]).contents().find('[name=selectedItem]');
                if (flushFields) {
                    $.each(selectedItems, function (rowIndex, item) {
                        $.each(flushFields, function (fieldIndex, flushFiled) {
                            let fieldsPair = flushFiled.split(':');
                            if (rowIndex === 0) {
                                $('#' + fieldsPair[0].trim()).val($(item).data(fieldsPair[1].trim()));
                            } else {
                                $('#' + fieldsPair[0].trim()).val($('#' + fieldsPair[0].trim()).val().trim() + ';' + $(item).data(fieldsPair[1].trim()));
                            }
                        });
                    })
                }

                if ($("#form").data('bootstrapValidator')) {
                    $("#form").data('bootstrapValidator').resetForm();
                }
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            },
            cancel: function (index, layero) {
                if ($("#form").data('bootstrapValidator')) {
                    $("#form").data('bootstrapValidator').resetForm();
                }
            },
            btn2: function (index, layero) {
                if (flushFields) {
                    $.each(flushFields, function (index, item) {
                        $('#' + item.split(':')[0]).val('');
                    });
                }
                if ($("#form").data('bootstrapValidator')) {
                    $("#form").data('bootstrapValidator').resetForm();
                }
            }
        });
    }

    initData();

    function initData(){
        var id = getUrlParam("id");
        if(id != ""){
            $.ajax({
                type : 'get',
                url : '/project/'+id,
                async : false,
                success : function(json) {
                    if (json.code === 0) {
                        var data = json.data;
                        $('#id').val(data.id);
                        $('#parentId').val(data.parentId);
                        $('#executorId').val(data.executorId);
                        $('#executorName').val(data.executorName);
                        $('#participantId').val(data.participantId);
                        $('#participantName').val(data.participantName);
                        $('#description').val(data.description);
                        $('#name').val(data.name);
                        $('#grade').val(data.grade);
                        $('#deadline').val(data.deadline);
                        $('#status').val(data.status);
                    }
                }
            });
        }
    }

    function save() {
        $('#form').bootstrapValidator();
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
            return;
        }

        var formdata = $("#form").serializeObject();

        $.ajax({
            type : 'put',
            url : '/project',
            contentType: "application/json; charset=utf-8",
            data : JSON.stringify(formdata),
            success : function(data) {
                if(data.code === 0){
                    layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                        parent.location.reload();
                    });
                }else {
                    layer.msg("修改失败", {shift: -1, time: 1000}, function () {
                    });
                }
            }
        });
    }

</script>
</body>
</html>
